package com.core.web.converters;


import java.text.DecimalFormat;
import java.util.Locale;

import org.apache.commons.beanutils.Converter;


public class DoubleConversor implements Converter
{
	private DecimalFormat f = null;

	public DoubleConversor(Locale locale)
	{
		f = (DecimalFormat) DecimalFormat.getInstance(locale);
	}

	@SuppressWarnings("unchecked")
	public Object convert(Class classe, Object valor)
	{
		try
		{
			if (valor != null && ((valor instanceof String) && !"".equals(valor) && (classe == Double.class || classe == Double.TYPE)))
			{
				return f.parse((String) valor);
			}
			if (valor == null)
				valor = 0.0d;
			return valor;
		}
		catch (Exception e)
		{
			return null;
		}
	}
}